package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.adapter.HomeRecyclerAdapter;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.ResultProduct;
import com.qtjf.qtmoneyservice.domain.ShiMingState;
import com.qtjf.qtmoneyservice.domain.Token;
import com.qtjf.qtmoneyservice.fragment.HomeFragment;
import com.qtjf.qtmoneyservice.fragment.MyselfFragment;
import com.qtjf.qtmoneyservice.fragment.ReZhengFragment;
import com.qtjf.qtmoneyservice.utils.RequestNetApi;
import com.qtjf.qtmoneyservice.widget.AimDialog;
import com.qtjf.qtmoneyservice.widget.UpdateDialog;

import java.util.HashMap;
import java.util.Map;

import impl.IHttpListener;

public class HomeActivity extends BaseActivity {


    TextView homeBt;
    TextView renZhengBt;
    TextView personalBt;

    ImageView homeImg;
    ImageView renZhengImg;
    ImageView personalImg;

    FragmentManager manager;
    FragmentTransaction transaction;
    HomeFragment homeFragment;
    ReZhengFragment renZhengFragment;
    MyselfFragment myselfFragment;
    Fragment currentFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        super.setTranslucent();
        WindowManager wm = this.getWindowManager();

        requestCall();
        getToken();

        //后面转移到启动页
        Constant.setSCREENWIDTH(wm.getDefaultDisplay().getWidth());
        Constant.setSCREENHEIGHT(wm.getDefaultDisplay().getHeight());
        Constant.setSCALE(this.getResources().getDisplayMetrics().density);

        UpdateDialog dialog=new UpdateDialog(this,R.style.myTransparent);
        dialog.show();

        initView(savedInstanceState);
        initData();
        initEvent();

    }

    private void initView(Bundle savedInstanceState) {
        homeBt = (TextView) findViewById(R.id.home_bt);
        renZhengBt = (TextView) findViewById(R.id.renzheng_bt);
        personalBt = (TextView) findViewById(R.id.personal_bt);

        homeImg = (ImageView) findViewById(R.id.home_img);
        renZhengImg = (ImageView) findViewById(R.id.renzheng_img);
        personalImg = (ImageView) findViewById(R.id.personal_img);

        homeFragment = new HomeFragment();
        renZhengFragment = new ReZhengFragment();
        myselfFragment = new MyselfFragment();


        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        if (savedInstanceState == null) {
            transaction.add(R.id.fragment_layout, homeFragment, "homeFragment");
            // transaction.replace(R.id.fragment_layout,fragmentA);
            transaction.commit();
            currentFragment = homeFragment;
        }


    }

    private void initData() {
    }

    private void initEvent() {

        homeBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAllViewNormal();
                homeBt.setTextColor(Color.parseColor("#4162ff"));
                homeImg.setImageResource(R.mipmap.home_img_press);

                transaction = manager.beginTransaction();
                if (homeFragment.isAdded()) {
                    if (currentFragment != homeFragment) {
                        transaction.show(homeFragment).hide(currentFragment).commit();
                        currentFragment = homeFragment;
                    }
                } else {
                    transaction.add(R.id.fragment_layout, homeFragment, "homeFragment").hide(currentFragment).commit();
                    //transaction.replace(R.id.fragment_layout,fragmentA);
                    currentFragment = homeFragment;

                }
            }
        });


        renZhengBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
// 开启Fragment事务
                setAllViewNormal();
                renZhengBt.setTextColor(Color.parseColor("#4162ff"));
                renZhengImg.setImageResource(R.mipmap.nearby_img_press);

                transaction = manager.beginTransaction();
                if (renZhengFragment.isAdded()) {
                    if (currentFragment != renZhengFragment) {
                        transaction.show(renZhengFragment).hide(currentFragment).commit();
                        currentFragment = renZhengFragment;
                    }
                } else {
                    transaction.add(R.id.fragment_layout, renZhengFragment, "renZhengFragment").hide(currentFragment).commit();
                    //transaction.replace(R.id.fragment_layout,fragmentA);
                    currentFragment = renZhengFragment;

                }
            }
        });


        personalBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setAllViewNormal();
                personalBt.setTextColor(Color.parseColor("#4162ff"));
                personalImg.setImageResource(R.mipmap.person_img_press);


                transaction = manager.beginTransaction();
                if (myselfFragment.isAdded()) {
                    if (currentFragment != myselfFragment) {
                        transaction.show(myselfFragment).hide(currentFragment).commit();
                        currentFragment = myselfFragment;
                    }
                } else {
                    transaction.add(R.id.fragment_layout, myselfFragment, "myselfFragment").hide(currentFragment).commit();
                    //transaction.replace(R.id.fragment_layout,fragmentA);
                    currentFragment = myselfFragment;

                }

                Intent intent = new Intent();
                intent.setAction("android.intent.action.test3");//action与接收器相同
                sendBroadcast(intent);
            }
        });


        homeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAllViewNormal();
                homeBt.setTextColor(Color.parseColor("#4162ff"));
                homeImg.setImageResource(R.mipmap.home_img_press);

                transaction = manager.beginTransaction();
                if (homeFragment.isAdded()) {
                    if (currentFragment != homeFragment) {
                        transaction.show(homeFragment).hide(currentFragment).commit();
                        currentFragment = homeFragment;
                    }
                } else {
                    transaction.add(R.id.fragment_layout, homeFragment, "homeFragment").hide(currentFragment).commit();
                    //transaction.replace(R.id.fragment_layout,fragmentA);
                    currentFragment = homeFragment;

                }
            }
        });



        renZhengImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAllViewNormal();
                renZhengBt.setTextColor(Color.parseColor("#4162ff"));
                renZhengImg.setImageResource(R.mipmap.nearby_img_press);

                transaction = manager.beginTransaction();
                if (renZhengFragment.isAdded()) {
                    if (currentFragment != renZhengFragment) {
                        transaction.show(renZhengFragment).hide(currentFragment).commit();
                        currentFragment = renZhengFragment;
                    }
                } else {
                    transaction.add(R.id.fragment_layout, renZhengFragment, "nearbyFragment").hide(currentFragment).commit();
                    //transaction.replace(R.id.fragment_layout,fragmentA);
                    currentFragment = renZhengFragment;

                }
            }
        });


        personalImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                setAllViewNormal();
                personalBt.setTextColor(Color.parseColor("#4162ff"));
                personalImg.setImageResource(R.mipmap.person_img_press);

                transaction = manager.beginTransaction();
                if (myselfFragment.isAdded()) {
                    if (currentFragment != myselfFragment) {
                        transaction.show(myselfFragment).hide(currentFragment).commit();
                        currentFragment = myselfFragment;
                    }
                } else {
                    transaction.add(R.id.fragment_layout, myselfFragment, "personalFragment").hide(currentFragment).commit();
                    //transaction.replace(R.id.fragment_layout,fragmentA);
                    currentFragment = myselfFragment;

                }

                Intent intent = new Intent();
                intent.setAction("android.intent.action.test3");//action与接收器相同
                sendBroadcast(intent);
            }
        });
    }


    private void setAllViewNormal() {
        homeBt.setTextColor(Color.parseColor("#999999"));
        renZhengBt.setTextColor(Color.parseColor("#999999"));
        personalBt.setTextColor(Color.parseColor("#999999"));


        homeImg.setImageResource(R.mipmap.home_img_normal);
        renZhengImg.setImageResource(R.mipmap.nearby_img_normal);
        personalImg.setImageResource(R.mipmap.person_img_normal);

    }

    private void requestCall() {
        String url= Constant.HOMEADDRESS + "users/getUserInfo?usermobile="+Constant.getUsermobile();
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Log.d("999",response);
                Gson gson=new Gson();
                if(response.length()>25) {
                    ShiMingState shiMingState = gson.fromJson(response, new TypeToken<ShiMingState>() {
                    }.getType());

                    Constant.setUserId(shiMingState.getData().getId());

                    if(shiMingState.getData().getIscidvalid().trim().equals("1"))
                    {
                        Constant.setShiMingState("1");
                    }
                    else
                    {
                        Constant.setShiMingState("0");
                    }
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(HomeActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                return map;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }


    private void getToken() {
        String url= Constant.HOMEADDRESS + "jdt/reqToken";
        final StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                if(response.length()>10) {

                    Gson gson=new Gson();
                    Token token = gson.fromJson(response, new TypeToken<Token>() {
                    }.getType());

                    Constant.setTOKEN(token.getToken());
                    Toast.makeText(HomeActivity.this, token.getToken(), Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(HomeActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("app_id",Constant.APPID);
                map.put("mobile_number",Constant.getUsermobile());
                return map;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }
}
